package org.gs.config;

import org.apache.cxf.ext.logging.event.LogEvent;
import org.apache.cxf.ext.logging.event.LogEventSender;
import org.noear.solon.annotation.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.namespace.QName;

/*
 * 用语csf日志文件的记录等操作
 * 
 */
@Component("customEventSender")
public class CustomEventSender implements LogEventSender{
    
 public static Logger log = LoggerFactory.getLogger(CustomEventSender.class);
    public static String SERVER_REQ_IN = "REQ_IN";
    public static String SERVER_RESP_OUT = "RESP_OUT";


    @Override
    public void send(LogEvent event) {
        String type = event.getType().toString();
        if (CustomEventSender.SERVER_REQ_IN.equals(type)) {
            log.info("=================================下面FYK要开始获取请求报文信息了=============================");
            printServerLog(event);
            log.info("=================================FYK获取请求报文信息完成了！=================================");
        } else if (CustomEventSender.SERVER_RESP_OUT.equals(type)) {
            log.info("=================================下面FYK要开始获取响应报文信息了=============================");
            printServerLog(event);
            log.info("=================================FYK获取响应报文信息完成了！=================================");
        }

    }

    public static void printServerLog(LogEvent event) {
        log.info(String.format("Type：%s", event.getType().toString()));
        log.info(String.format("Address：%s", event.getAddress()));
        log.info(String.format("HttpMethod：%s", event.getHttpMethod()));
        log.info(String.format("Content-Type：%s", event.getContentType()));
        log.info(String.format("ResponseCode：%s", event.getResponseCode()));
        log.info(String.format("ExchangeId：%s", event.getExchangeId()));
        log.info(String.format("MessageId：%s", event.getMessageId()));
        if (event.getServiceName() != null) {
            log.info(String.format("ServiceName：%s", localPart(event.getServiceName())));
            log.info(String.format("PortName：%s", localPart(event.getPortName())));
            log.info(String.format("PortTypeName：%s", localPart(event.getPortTypeName())));
        }
        if (event.getFullContentFile() != null) {
            log.info("FullContentFile：%s", event.getFullContentFile().getAbsolutePath());
        }
        log.info("Headers:", event.getHeaders().toString());
        log.info(String.format("报文：%s", event.getPayload()));
    }

    public static String localPart(QName name) {
        return name == null ? null : name.getLocalPart();
    }
    
}